package com.seeyon.ctp.rest.resources;

import com.seeyon.apps.collaboration.manager.ColManager;
import com.seeyon.apps.collaboration.po.ColSummary;
import com.seeyon.apps.kkSample.manager.KKEsignManager;
import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.sql.SQLException;
import java.util.Map;

/**
 * @Author: L
 * @Description: 电子签章接口
 * @Date: 创建于10:18 2022-10-24
 **/
@Path("/kkSample")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class kkEsignResource extends BaseResource {

    private static final Log log = CtpLogFactory.getLog(kkEsignResource.class);
    private ColManager colManager = (ColManager) AppContext.getBean("colManager");
    private FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
    private KKEsignManager kkEsignManager = (KKEsignManager) AppContext.getBean("kkEsignManager");

    /**
     * @param params
     * @return
     */
    @POST
    @Path("doSignature")
    public Response doSignature(Map<String, Object> params) {
        log.info("进入签章接口");
        try {
            if (params.get("form") != null && params.get("formData") != null) {
                /**
                 * 获取表单数据对象
                 */
                //表单ID
                Long formId = Long.valueOf(params.get("form").toString());
                //表单数据ID
                Long formDataId = Long.valueOf(params.get("formData").toString());
                //表单对象
                FormBean formBean = formApi4Cap4.getForm(formId);
                //表单数据对象
                FormDataMasterBean formDataMasterBean = formApi4Cap4.getSessioMasterDataBean(formDataId);
                if (formDataMasterBean == null) {
                    formDataMasterBean = formApi4Cap4.findDataById(formDataId, formBean, null);
                    if (formDataMasterBean == null) {
                        ColSummary summary = colManager.getColSummaryById(formDataId);
                        if (summary != null) {
                            formDataMasterBean = formApi4Cap4.findDataById(summary.getFormRecordid(), formBean, null);
                            if (formDataMasterBean == null) {
                                log.info("契约锁电子签章：获得表单数据对象失败！");
                                return fail("契约锁电子签章：获得表单数据对象失败！");
                            }
                        } else {
                            log.info("契约锁电子签章：获得表单数据对象失败！");
                            return fail("契约锁电子签章：获得表单数据对象失败！");
                        }
                    }
                }
                log.info("获取到表单对象：" + formBean + "表单数据对象:" + formDataMasterBean);
                /**
                 * 发起签署
                 */
                Map<String, Object> resultMap = kkEsignManager.doSignature(formBean, formDataMasterBean);
                log.info("发起完签署");
                /**
                 * 判断签章是否成功
                 */
                if ("0".equals(MapUtils.getString(resultMap, "code"))) {
                    return success(resultMap);
                } else {
                    return fail(MapUtils.getString(resultMap, "message"));
                }
            }
            log.info("e签宝电子签章：表单Id和表单数据Id为空！");
            return fail("进行电子签异常.");
        } catch (SQLException | BusinessException e) {
            log.error("契约锁电子签章：获得表单数据对象异常！" + e.getMessage());
            return fail("契约锁电子签章：获得表单数据对象异常！");
        }
    }
}
